package org.pi.common.plot.axes

import org.pi.common.plot.drawables.Drawable

trait BaseAxes extends Axes with org.pi.common.observerpattern.ObservableImpl {
  protected var elements = scala.collection.mutable.ListBuffer[Drawable]()
  def addDrawable(drawable: Drawable) { 
	elements.append(drawable) 
	drawable.addObserver(this)
	notifyObservers()
  }
  def clear {
	  //TODO remove observers form list
	elements.clear 
	notifyObservers()
  }
  def notifiedByObservable(obs: org.pi.common.observerpattern.Observable) {
	notifyObservers()
  }
  
  private val connectedAxes = scala.collection.mutable.Set[Axes]()
  override def connectToAxes(other: Axes): Unit = {
	  if (!connectedAxes.contains(other)) {
		  connectedAxes += other
		  other connectToAxes(this)
	  }
  }

  	private var selectPointCalled : Boolean = false;
	override def selectPoint(x:Float, y:Float) : Unit = {
		if (selectPointCalled==false) {
			selectPointCalled=true
			connectedAxes.foreach( _.selectPoint(x,y) )
			selectPointCalled=false
		}
	}

	private var selectRegionCalled : Boolean = false;
	override def selectRegion(x1:Float, y1:Float,x2:Float, y2:Float) : Unit = {
		if (selectRegionCalled==false) {
			selectRegionCalled=true
			connectedAxes.foreach( _.selectRegion(x1,y1,x2,y2) )
			selectRegionCalled=false
		}
	}
	private var zoomCalled : Boolean = false;
	override def zoom(x1:Float, y1:Float,x2:Float, y2:Float) : Unit = {
		if (zoomCalled==false) {
			zoomCalled=true
			connectedAxes.foreach( _.zoom(x1,y1,x2,y2) )
			zoomCalled=false
		}
	}
	private var resetZoomCalled : Boolean = false;
	override def resetZoom(): Unit = {
		if (resetZoomCalled==false) {
			resetZoomCalled=true
			connectedAxes.foreach( _.resetZoom() )
			resetZoomCalled=false
		}
	}

}
